<?php
namespace app\controller\v0;

use think\facade\View;

use app\controller\Base;
use app\model\ProductsWire;
use app\model\ProductsSurface;
use app\model\ProductsWearPlate;
use app\model\Honor;
use app\model\HonorType;
use app\utils\ReUtils;
use app\model\GuestBook;

class Index extends Base
{
    public function index()
    {
        return View::fetch();
    }

    public function hello()
    {
        return '正在建设中.....';
    }

    public function about()
    {
        return View::fetch();
    }

    public function contactus()
    {
        return View::fetch();
    }

    public function wire()
    {
        $list_wire0 = [];
        $list_wire1 = [];
        $list_wire2 = [];

        $list_wire = ProductsWire::order('sort desc')->select();
        
        foreach ($list_wire as $value) {
            if ($value->wire_type==1) {
                $list_wire1[] = $value;
            } elseif ($value->wire_type==2) {
                $list_wire2[] = $value;
            } else {
                $list_wire0[] = $value;
            }
        }

        $list = [
            [
                'name' => '耐腐蚀热喷涂材料',
                'list' => $list_wire0,
                'wire_type' => 0
            ],
            [
                'name' => '耐磨热喷涂材料',
                'list' => $list_wire1,
                'wire_type' => 1
            ],
            [
                'name' => '打底热喷涂材料',
                'list' => $list_wire2,
                'wire_type' => 2
            ]
        ];

        View::assign('list', $list);

        return View::fetch();
    }

    public function surface()
    {
        $list = ProductsSurface::select();

        View::assign('list', $list);

        return View::fetch();
    }

    public function wearPlate()
    {
        $list = ProductsWearPlate::select();

        View::assign('list', $list);

        return View::fetch();
    }

    public function test()
    {
        return "this is test";
    }

        /**
     * 留言板
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function saveGuestBook()
    {
        if ($this->request->isPost()) {
            $data = [
                'name'    => $this->request->param('name', '', 'strip_tags'),
                'phone'   => $this->request->param('phone', '', 'strip_tags'),
                'content' => $this->request->param('content', '', 'strip_tags'),
                "company" => $this->request->param('company', '', 'strip_tags'),
                "industry"=> $this->request->param('industry', '', 'strip_tags'),
                "city"    => $this->request->param('city', '', 'strip_tags'),
                "email"   => $this->request->param('email', '', 'strip_tags'),
                'time'    => time()
            ];

            if ($data['name'] == '') {
                return ReUtils::error("姓名不能为空");
            }

            if ($data['content'] == '') {
                return ReUtils::error("留言内容不能为空");
            }

            if (preg_phone($data['phone']) == false) {
                return ReUtils::error("手机号格式不正确");
            }

            $check = check_token($this->request->param());

            if (false === $check) {
                return ReUtils::error("验证有误");
            }

            try {
                GuestBook::saveGuestBook($data);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                return ReUtils::error($msg);
            }
            return ReUtils::success();
        }
    }

    public function token()
    {
        $token = $this->request->buildToken('__token__', 'sha1');
        
        return $token;
    }
}
